﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.Text;

using Danser_AJAXEnabledWebApplication.User_Controls;
using Danser_Entity.Entities;

namespace Danser_AJAXEnabledWebApplication.PageExt.Utilities
{
    /// <summary>
    /// Help formatting page UI elements 
    /// </summary>
    public class PageFormatter
    {
        /// <summary>
        /// Change the text of a button 
        /// </summary>
        /// <param name="button">the button to change text on</param>
        /// <param name="domain">must include exactly 2 strings</param>
        public void ToggleButtonText(Button button, String[] domain)
        {
            button.Text =
                button.Text.Equals(domain[0]) ?
                domain[1] :
                domain[0];
        }

        /// <summary>
        /// Change the text of a link button 
        /// </summary>
        /// <param name="button">the button to change text on</param>
        /// <param name="domain">must include exactly 2 strings</param>
        public void ToggleButtonText(LinkButton button, String[] domain)
        {
            button.Text =
                button.Text.Equals(domain[0]) ?
                domain[1] :
                domain[0];
        }

        /// <summary>
        /// Display only columns selected in the template pattern.
        /// <para>This method uses regular expressions</para>
        /// </summary>
        /// <param name="grid">The grid with columns to display in list</param>
        /// <param name="list">The list</param>
        /// <param name="startIndex">grid columns index to start counting from</param>
        /// <param name="template">The string pattern consisting of columns to display. Must be in regular expression pattern format
        /// if null, considers all columns</param>
        public void DisplayColumnsTemplate(GridView grid, MultiSelectList list, int startIndex, String template)
        {
            Regex pattern =
                template != null ?
                new Regex(template) :
                new Regex("()*");       //allways true

            //feed columns list
            for (int i = startIndex; i < grid.Columns.Count; i++)
            {
                String headerText = grid.Columns[i].HeaderText;
                
                //show only the pattern columns
                grid.Columns[i].Visible = pattern.IsMatch(headerText);

                //if list sent to method - update its items
                if (list != null)
                {
                    ListItem item = new ListItem(headerText);

                    //mark as selected all visible columns
                    item.Selected = grid.Columns[i].Visible;
                    list.Items.Add(item);
                }

            }
        }

        public void DisplayColumnsTemplate(GridView grid, MultiSelectList list, int startIndex)
        {
            DisplayColumnsTemplate(grid, list, startIndex, null);
        }

        public void DisplayColumnsTemplate(GridView grid, String template)
        {
            DisplayColumnsTemplate(grid, null, 1, template);
        }

        /// <summary>
        /// Filter columns in data grid according to selected items in list
        /// </summary>
        /// <param name="grid">the grid displaying the table's data</param>
        /// <param name="list">list containing the table's columns</param>
        public void FilterColumnsByListSelection(GridView grid, MultiSelectList list)
        {
            if (grid == null || list == null || grid.EditIndex > -1)
                return;

            int gap = grid.Columns.Count - list.Items.Count;

            for (int i = 0; i < list.Items.Count; i++)
            {
                // unique match between index in list to grid's column index
                grid.Columns[i + gap].Visible =
                    list.Items[i].Selected ?
                    true :
                    false;
            }

            list.HideSelectionList();
        }

        /// <summary>
        /// Set input parameters to graph and refreshes the graph's content 
        /// </summary>
        /// <param name="graphBuilder"></param>
        /// <param name="testIndex">current test index</param>
        /// <param name="anchors">anchor ids for comparing</param>
        public void SetGraphInput(IGraphBuilder graphBuilder, String testIndex, IList<String> anchors)
        {
            if (String.IsNullOrEmpty(testIndex))
                return;

            graphBuilder.SetAnchors(anchors);
            graphBuilder.SetTestIndex(testIndex);

            graphBuilder.DrawGraph();
        }

        public void SetGraphInput(IGraphBuilder graphBuilder, String testIndex)
        {
            SetGraphInput(graphBuilder, testIndex, null);
        }

        /// <summary>
        /// Place in text box all failed validations. Each one in a new row
        /// </summary>
        /// <param name="failedValidationsTextBox"></param>
        /// <param name="failedValidations"></param>
        public void FormatFailedValidations(TextBox failedValidationsTextBox, IList<ValidationTemplate> failedValidations)
        {
            //max 3 rows in grid cell
            failedValidationsTextBox.Rows = Math.Min(3, failedValidations.Count);
            if (failedValidationsTextBox.Rows > 1)
                failedValidationsTextBox.TextMode = TextBoxMode.MultiLine;

            StringBuilder validationsString = new StringBuilder();

            //add the validations' names to text box
            foreach (ValidationTemplate failedValidation in failedValidations)
            {
                validationsString.Append(failedValidation.Name);
                validationsString.Append("\n");
            }

            failedValidationsTextBox.Text = validationsString.ToString();

        }
    }
}
